The plots below are interactive. Feel free to zoom and rotate them.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sb
from sklearn import metrics
from sklearn.model_selection import train_test_split
from numpy import random
import plotly.express as px
%matplotlib inline
Prediction function
def predict(X,W):
thresh=1
pred=np.dot(X,W)
l=[1 if i>=thresh else 0 for i in pred]
return l
def accuracy(X,W,y):
if(len(X)!=len(y)):
print("Number of X records and y tarets are not equal!")
return
crct=0
pred=predict(X,W)
for i in range(len(y)):
if(y[i]==pred[i]):
crct+=1
return crct/len(X)
def train(X,y,epochs=100,lr=0.05):
w=random.rand(X.shape[1])
w=w.reshape(X.shape[1],1)
for i in range(1,len(w.T[0])):
w.T[0][i]=0
w.T[0][0]=1
print("Initial weights: ")
print(w)
lr=np.array([[lr]])
for e in range(epochs):
acc=accuracy(X,w,y)
pred=predict(X,w)
df=pd.DataFrame(X,columns=['x1','x2','x3'])
df['x4']=pred
#print(df)
fig = px.scatter_3d(df, x='x1', y='x2', z='x3',color='x4')
fig.show()
print("Epoch: "+str(e)+" Accuracy: "+str(acc))
if(acc==1):
print("Reached accuracy of 100%. Stopping now...")
return w
error=y-pred
error=error*lr
#print(error)
#error=error.reshape(1,len(error))
#print(w)
temp=X.T*error
#print(temp)
for i in range(len(temp.T)):
w.T[0]=w.T[0]+temp.T[i]
#print("--")
return w
#x=np.array([[1,2,3,4],[5,6,7,8]])
x=np.array([[1,1,1],[1,1,-1],[1,0,-1],[1,-1,-1],[1,-1,1],[1,0,1]])
w=np.array([1,2,3,4])
y=np.array([1,1,1,0,0,0])
w=train(x,y)
Initial weights: [[1.] [0.] [0.]]
Epoch: 0 Accuracy: 0.5
Epoch: 1 Accuracy: 0.5
Epoch: 2 Accuracy: 1.0 Reached accuracy of 100%. Stopping now...
So it reached accuracy of 100% in the 3rd iteration itself.
The weights are:
w
array([[ 1. ],
[ 0.2],
[-0.1]])
from sklearn.datasets import load_iris
iris = load_iris()
data1 = pd.DataFrame(data= np.c_[iris['data'], iris['target']],
columns= iris['feature_names'] + ['target'])
data1 = data1[data1.target!=2]
data1
| sepal length (cm) | sepal width (cm) | petal length (cm) | petal width (cm) | target | |
|---|---|---|---|---|---|
| 0 | 5.1 | 3.5 | 1.4 | 0.2 | 0.0 |
| 1 | 4.9 | 3.0 | 1.4 | 0.2 | 0.0 |
| 2 | 4.7 | 3.2 | 1.3 | 0.2 | 0.0 |
| 3 | 4.6 | 3.1 | 1.5 | 0.2 | 0.0 |
| 4 | 5.0 | 3.6 | 1.4 | 0.2 | 0.0 |
| ... | ... | ... | ... | ... | ... |
| 95 | 5.7 | 3.0 | 4.2 | 1.2 | 1.0 |
| 96 | 5.7 | 2.9 | 4.2 | 1.3 | 1.0 |
| 97 | 6.2 | 2.9 | 4.3 | 1.3 | 1.0 |
| 98 | 5.1 | 2.5 | 3.0 | 1.1 | 1.0 |
| 99 | 5.7 | 2.8 | 4.1 | 1.3 | 1.0 |
100 rows × 5 columns
X=data1.drop(['target','petal width (cm)'],axis=1)
y=data1['target']
from sklearn.linear_model import Perceptron
clf = Perceptron()
clf.fit(X, y)
Perceptron()
clf.score(X,y)
1.0
X
| sepal length (cm) | sepal width (cm) | petal length (cm) | |
|---|---|---|---|
| 0 | 5.1 | 3.5 | 1.4 |
| 1 | 4.9 | 3.0 | 1.4 |
| 2 | 4.7 | 3.2 | 1.3 |
| 3 | 4.6 | 3.1 | 1.5 |
| 4 | 5.0 | 3.6 | 1.4 |
| ... | ... | ... | ... |
| 95 | 5.7 | 3.0 | 4.2 |
| 96 | 5.7 | 2.9 | 4.2 |
| 97 | 6.2 | 2.9 | 4.3 |
| 98 | 5.1 | 2.5 | 3.0 |
| 99 | 5.7 | 2.8 | 4.1 |
100 rows × 3 columns
w=train(X.values,y.values)
Initial weights: [[1.] [0.] [0.]]
Epoch: 0 Accuracy: 0.5
Epoch: 1 Accuracy: 0.5
Epoch: 2 Accuracy: 0.5
Epoch: 3 Accuracy: 0.5
Epoch: 4 Accuracy: 0.5
Epoch: 5 Accuracy: 0.5
Epoch: 6 Accuracy: 0.5
Epoch: 7 Accuracy: 0.97
Epoch: 8 Accuracy: 1.0 Reached accuracy of 100%. Stopping now...
The model made from scratch performs just as well as the built-in perceptron model in SciKit-learn. It reached an accuracy of 100% in just 9 iterations.